package com.registration.server.impl;

import com.registration.entity.ResultEntity;
import com.registration.entity.YlRegistrationRecord;
import com.registration.entity.utilentity.UEeRegistrationReconrdController;
import com.registration.mapper.YlRegistrationRecordMapper;
import com.registration.server.RegistrationRecordServer;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class RegistrationRecordServerImpl implements RegistrationRecordServer {

    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    YlRegistrationRecordMapper ylRegistrationRecordMapper;

    RedisTemplate<String, Object> redistemplate = new RedisTemplate();

    //设置公共头标签存入redis中
    String DDkey = "DDGH";

    //数据放入redis暂存（无支付状态）
    @Override
    public String addRecord(YlRegistrationRecord ylRegistrationRecord) {

        ValueOperations<String, Object> opsForValue = redistemplate.opsForValue();
        String key = DDkey+UUID.randomUUID().toString().replace("-","");
        opsForValue.set(key,ylRegistrationRecord);

        //向队列发送消息
        rabbitTemplate.convertAndSend("appointment-register", "sms", key);

        return key;
    }

    //改变支付状态
    @Override
    public void confirmRecord(UEeRegistrationReconrdController uEe) {

        ValueOperations<String, Object> opsForValue = redistemplate.opsForValue();

        YlRegistrationRecord ylRegistrationRecord = (YlRegistrationRecord)opsForValue.get(uEe.getVerifykey());
        ylRegistrationRecord.setRegistrationPaySign(uEe.getState());

        opsForValue.set(uEe.getVerifykey(),ylRegistrationRecord);
    }

    @Override
    public int depositSqlRecord(String key) {

        ValueOperations<String, Object> opsForValue = redistemplate.opsForValue();

        YlRegistrationRecord ylRegistrationRecord = (YlRegistrationRecord) opsForValue.get(key);

        return ylRegistrationRecordMapper.addRecord(ylRegistrationRecord);
    }

    @Override
    public List<YlRegistrationRecord> findAll(YlRegistrationRecord ylRegistrationRecord) {
        return ylRegistrationRecordMapper.findAll(ylRegistrationRecord);
    }

    //待支付单查询
    @Override
    public YlRegistrationRecord toBePaid(UEeRegistrationReconrdController uEe) {

        ValueOperations<String, Object> opsForValue = redistemplate.opsForValue();

        return (YlRegistrationRecord) opsForValue.get(uEe.getVerifykey());
    }
    //已就诊接口
    @Override
    public List<YlRegistrationRecord> endSeeDoctor() {
        return ylRegistrationRecordMapper.endSeeDoctor();
    }

    @Override
    public List<YlRegistrationRecord> unSeeDoctor() {
        return ylRegistrationRecordMapper.unSeeDoctor();
    }

    @Override
    public List<YlRegistrationRecord> unPaid() {
        return ylRegistrationRecordMapper.unPaid();
    }

}
